/**
    @Author         :   Yehuda Roth
     Create Date    :   18/06/2010
     Last Modified  :   26/06/2010
    @Brief          :   Single Threaded Schedular. scheduler of tasks.
*/
#ifndef STSCHEDULAR_H_INCLUDED
#define STSCHEDULAR_H_INCLUDED

typedef struct Scheduler_t Scheduler;

/*  Create scheduler, accept _size of the Q of tasks to run. On success return scheduler pointer , on failure return 0. *
 *  Failure can occure when allocation fails.                                                                           */
Scheduler*  SchedCreate(int _size);

void SchedDestroy(Scheduler* _sc);

/*  Run tasks for every period. The task is given by _func, if it return -1 it's gotten out from scheduler. void* _data *
 *  is optional for giving state to function. On success return 0, failure -1 and prints to stderr. Error can occure on *
 *  allocations failure.                                                                                                */
int SchedRunEvery(Scheduler* _sc, int (*_func)(void*) , int _period, void* _data);

/*  Run the task one time and update his next run. On success return 0, on failure -1. Failure occure when there no     *
 *  tasks to run in scheduler.                                                                                          */
int SchedRunOnce(Scheduler* _scheduler);

/*  Calculate and return the timeout till next task. return the timeval struct which declared:                          *
 *                                                                           struct timeval                             *
 *                                                                           {                                          *
 *                                                                               suseconds tv_sec;                      *
 *                                                                               suseconds tv_usec;                     *
 *                                                                           }                                          *
 *  If next run of task is passed return the args of struct zeroed, if there no tasks to run return tv_sec == -1        */
struct timeval SchedGetTimeOut(Scheduler* _scheduler);

/*  Start the running of scheduler. If the scheduler is empty return -1 and prints to stderr.                           */
int SchedStart(Scheduler* _scheduler);

#endif /* STSCHEDULAR_H_INCLUDED    */
